/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.coupon.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.activity.entity.VCard;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.ShareEnum;
import com.ykht.proj.coupon.entity.*;
import com.ykht.proj.coupon.mapper.FxRewardPersonMapper;
import com.ykht.proj.coupon.mapper.FxShareSingleMapper;
import com.ykht.proj.coupon.service.FxShareSingleService;
import com.ykht.proj.coupon.vo.BaseSearchTimeVo;
import com.ykht.proj.coupon.vo.ShareSingle4C;
import com.ykht.proj.coupon.vo.ShareSingle4CStatistics;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 普通分享资讯表（去重后）
 *
 * @author proj code generator
 * @date 2021-08-12 14:10:39
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class FxShareSingleServiceImpl extends ServiceImpl<FxShareSingleMapper, FxShareSingle> implements FxShareSingleService {

	private final FxShareSingleMapper fxShareSingleMapper;
	private final FxRewardPersonMapper fxRewardPersonMapper;

	@Override
	public ShareSingle4CStatistics shareSinglePage(Page<ShareSingle4C> page, Integer type, BaseSearchTimeVo searchTimeVo) {
		ShareSingle4CStatistics c = new ShareSingle4CStatistics();
		page = fxShareSingleMapper.shareSinglePage(page,type, SecurityUtils.getPersonId(),searchTimeVo);
		Set<Long> brandIds = new HashSet<>();
		Set<Long> shopIds = new HashSet<>();
		Set<Long> cmsIds = new HashSet<>();
		Set<Long> posterIds = new HashSet<>();
		Set<Long> cardIds = new HashSet<>();

		page.getRecords().stream().forEach(o->{
			if (o.getTatId()!=null) {
				switch (ShareEnum.getThisByVal(o.getType())) {
					case brand:
						brandIds.add(o.getTatId());
						break;
					case shop:
						shopIds.add(o.getTatId());
						break;
					case poster:
						posterIds.add(o.getTatId());
						break;
					case cms:
						cmsIds.add(o.getTatId());
						break;
					case card:
						cardIds.add(o.getTatId());
						break;
				}
			}
		});
		Map<Long, String> brandMap = new HashMap<>();
		Map<Long, String> shopMap = new HashMap<>();
		Map<Long, String> postMap = new HashMap<>();
		Map<Long, String> cmsMap = new HashMap<>();
		Map<Long, String> cardMap = new HashMap<>();
		if (brandIds.size() != 0) brandMap = new CnBrand().selectList(Wrappers.lambdaQuery(CnBrand.class).select(CnBrand::getId,CnBrand::getBrandName).in(CnBrand::getId, brandIds)).stream().collect(Collectors.toMap(CnBrand::getId, CnBrand::getBrandName));
		if (shopIds.size() != 0) shopMap = new CnShop().selectList(Wrappers.lambdaQuery(CnShop.class).select(CnShop::getId, CnShop::getName).in(CnShop::getId, shopIds)).stream().collect(Collectors.toMap(CnShop::getId, CnShop::getName));
		if (posterIds.size() != 0) postMap = new FxConfPosterBrand().selectList(Wrappers.lambdaQuery(FxConfPosterBrand.class).select(FxConfPosterBrand::getBrandId, FxConfPosterBrand::getName).in(FxConfPosterBrand::getBrandId, posterIds)).stream().collect(Collectors.toMap(FxConfPosterBrand::getBrandId, FxConfPosterBrand::getName));
		if (cmsIds.size() != 0) cmsMap = new FxCmsInfo().selectList(Wrappers.lambdaQuery(FxCmsInfo.class).select(FxCmsInfo::getId, FxCmsInfo::getPcTitle).in(FxCmsInfo::getId, cmsIds)).stream().collect(Collectors.toMap(FxCmsInfo::getId, FxCmsInfo::getPcTitle));
		if (cardIds.size() != 0) cardMap = new VCard().selectList(Wrappers.lambdaQuery(VCard.class).select(VCard::getId, VCard::getName).in(VCard::getId, cardIds)).stream().collect(Collectors.toMap(VCard::getId, VCard::getName));


		Map<Long, String> finalBrandMap = brandMap;
		Map<Long, String> finalShopMap = shopMap;
		Map<Long, String> finalPostMap = postMap;
		Map<Long, String> finalCmsMap = cmsMap;
		Map<Long, String> finalCardMap = cardMap;
		page.getRecords().stream().forEach(o->{
			if (o.getTatId()!=null) {
				switch (ShareEnum.getThisByVal(o.getType())) {
					case brand:
						o.setName(finalBrandMap.get(o.getTatId()));
						break;
					case shop:
						o.setName(finalShopMap.get(o.getTatId()));
						break;
					case poster:
						o.setName(finalPostMap.get(o.getTatId()));
						break;
					case cms:
						o.setName(finalCmsMap.get(o.getTatId()));
						break;
					case card:
						o.setName(finalCardMap.get(o.getTatId()));
						break;
				}
			}
		});

		Integer visitorNum = fxShareSingleMapper.visitorNum(type,SecurityUtils.getPersonId());
		Integer reciveNum = fxShareSingleMapper.reciveNum(type,SecurityUtils.getPersonId());

		c.setBrowseNum(page.getTotal());
		c.setReciveNum(reciveNum);
		c.setVisitorNum(visitorNum);
		c.setPage(page);
		return c;
	}
}
